package com.example.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.dto.SetmealDto;
import com.example.ruiji.pojo.Dish;
import com.example.ruiji.pojo.R;
import com.example.ruiji.pojo.Setmeal;
import com.example.ruiji.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    //新增套餐
   @PostMapping
    public R<String> save(@RequestBody  SetmealDto SetmealDto)
    {
        //访问层来接收数据  ->2个表 一个是套餐表 保存套餐信息 一个是套餐商品关联表
        setmealService.saveWithDish(SetmealDto);

        return R.success("提交成功");
    }

    //分页处理
    @GetMapping("/page")
    public  R<Page> page(int page, int pageSize, String name)
    {

        R<Page> pageg = setmealService.pageg(page, pageSize, name);
        return pageg;
    }
///批量删除
    @DeleteMapping
public  R<String> delete(@RequestParam List <Long> ids)

{

    setmealService.delet(ids);
    return R.success("删除成功");
}
@GetMapping("/list")
    public R<List<Setmeal>> list(  Setmeal setmeal)
{
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
    queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
    queryWrapper.orderByDesc(Setmeal::getUpdateTime);

    List<Setmeal> list = setmealService.list(queryWrapper);

    return R.success(list);

}
    @PostMapping("/status/{id}")
    public R<String> up(@RequestParam List<Long> ids,@PathVariable("id") int id)
    {
        //进行更新即可
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
       Setmeal dish=new Setmeal();
        dish.setStatus(id);
        setmealService.update(dish,queryWrapper);
        return R.success("停售成功");
    }
}
